from django.db import models
from django.contrib.contenttypes.models import ContentType
from django.contrib.contenttypes import generic


class Attribute(models.Model):
    content_type = models.ForeignKey(ContentType)
    object_id = models.PositiveIntegerField()
    content_object = generic.GenericForeignKey('content_type', 'object_id')

    key = models.CharField(max_length=50, db_index=True)

    type = models.CharField(max_length=50)

    def __init__(self, *args, **kwargs):
        super(Attribute, self).__init__(*args, **kwargs)

	if not self.type:
		self.type = self.__class__.__name__.lower()

    def __unicode__(self):
        return u'%s: %s' % (self.content_object, self.key)


class IntegerAttribute(Attribute):
    value = models.IntegerField()


class FloatAttribute(Attribute):
    value = models.FloatField()


class StringAttribute(Attribute):
    value = models.CharField(max_length=100, blank=True)


class DateRangeAttribute(Attribute):
    start = models.DateField()
    end = models.DateField()


def model_for_type(t):
    if t == int:
        return IntegerAttribute
    elif t == float:
        return FloatAttribute
    else:
        return StringAttribute
